﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Control
{
    public class ValidatorUtil
    {
        public static string GenerateValidatorJsCode(string validatorForm, string handle)
        {
            var js = @" var isValid = true;
                            var dom = {0}.getForm().dom;
                            var count = dom.length;
                            for (var i = 0; i < count; i++) {
                                var c = dom[i];
                                var ec = Ext.getCmp(c.id);
                                if(ec && {0}.el.contains(ec.el)) {
                                    if (ec.isValid) {
                                        if (!ec.isValid() && isValid) {
                                            isValid = false;
                                        }
                                    }
                                }
                            }
                            if (!isValid) {
                                return false;
                            }";

            return js.Replace("{0}", validatorForm) + handle;
        }
    }
}
